﻿global DESKIDX := 1
global NUMBER_TOGGLE := 0

;qwerty-to-colemak.ahk

;这个AutoHotKey脚本的作用是把标准的Qwerty键盘布局映射成Colemak键盘布局

;Colemak Keyboard Layout
;---------------------------------------------
;  ~  !  @  #  $  %  ^  &  *  (  )  _  +  ____
;  `  1  2  3  4  5  6  7  8  9  0  -  =   BS
; ___                                {  }  |
; Tab  q  w  f  p  g  j  l  u  y  ;  [  ]  \
; ____                                "  _____
; Caps  a  r  s  t  d  h  n  e  i  o  '  Enter
; _____                       <  >  ?  _______
; Shift  z  x  c  v  b  k  m  ,  .  /   Shift

;Qwerty Keyboard Layout
;---------------------------------------------
;  ~  !  @  #  $  %  ^  &  *  (  )  _  +  ____
;  `  1  2  3  4  5  6  7  8  9  0  -  =   BS
; ___                                {  }  |
; Tab  q  w  e  r  t  y  u  i  o  p  [  ]  \
; ____                             :  "  _____
; Caps  a  s  d  f  g  h  j  k  l  ;  '  Enter
; _____                       <  >  ?  _______
; Shift  z  x  c  v  b  n  m  ,  .  /   Shift

;   q  w  e  r  t  y  u  i  o  p  [  ]  \
;=> q  w  f  p  g  j  l  u  y  ;  [  ]  \

e::f
r::p
t::g
y::j
u::l
i::u
o::y
p::;

;   a  s  d  f  g  h  j  k  l  ;  '
;=> a  r  s  t  d  h  n  e  i  o  '
+a::A
+s::R
+d::S
+f::T
+g::D
+h::H
+j::N
+k::E
+l::I
+`;::O
; a::
; if(NUMBER_TOGGLE == 1) {
;     send, 1
; }else {
;     send, a
; }
; return
; s::
; if(NUMBER_TOGGLE == 1) {
;     send, 2
; }else {
;     send, r
; }
; return
; d::
; if(NUMBER_TOGGLE == 1) {
;     send, 3
; }else {
;     send, s
; }
; return
; f::
; if(NUMBER_TOGGLE == 1) {
;     send, 4
; }else {
;     send, d
; }
; return
; g::
; if(NUMBER_TOGGLE == 1) {
;     send, 5
; }else {
;     send, d
; }
; return
; h::
; if(NUMBER_TOGGLE == 1) {
;     sendinput, 6
; }else {
;     send, h
; }
; return
; j::
; if(NUMBER_TOGGLE == 1) {
;     send, 7
; }else {
;     send, n
; }
; return
; k::
; if(NUMBER_TOGGLE == 1) {
;     send, 8
; }else {
;     send, e
; }
; return
; l::
; if(NUMBER_TOGGLE == 1) {
;     send, 9
; }else {
;     send, i
; }
; return
; `;::
; if(NUMBER_TOGGLE == 1) {
;     send, 0
; }else {
;     send, o
; }
; return
a::a
s::r
d::s
f::t
g::d
h::h
j::n
k::e
l::i
`;::o

;   z  x  c  v  b  n  m  ,  .  /
;=> z  x  c  v  b  k  m  ,  .  /
n::k

#Enter::
    Run, wt
return

#q::
    Send, #1
return

#w::
    Send, #2
return

#f::
    Send, #3
return

#r::
    Send, #4
return

; #Space::
;     Send, #{Up}
; return

!v::
    Send, cd{Space}^v
return

+Enter::
    ; send, {Shift}
    ; return
    ; Send, {Shift}
    Send, #{Space}
    ; Send, {Shift}
return

; numbers

;Esc
Esc::Esc
!Escape::
return
Esc & a::reload
Esc & c::Send, ^c
Esc & d::#l
Esc & f::#3
; Esc & e::Send, {Win down}3
;Esc & f::Send, !^x
Escape & f::Send, !^x
Esc & g::#d
Esc & j::#1
Esc & k::#2
Esc & l::#3
; Esc & h::#4
Esc & h::Esc
^h::Click
; ^i::Send, {Blind}{Up}
; ^j::Send, {Blind}{Left}
; ^k::Send, {Blind}{Down}
; ^l::Send, {Blind}{Right}
^i::Send, {Up}
^j::Send, {Left}
^k::Send, {Down}
^l::Send, {Right}
Esc & m::Esc
Esc & n::#6
Esc & o::Esc
Esc & p::Esc
Esc & q::#1
Esc & r::#4
Esc & s::
test()
return
Join(sep, params*) {
    for index,param in params
        str .= param . sep
    return SubStr(str, 1, -StrLen(sep))
}
test() {
    MsgBox % Join("`n", "one", "two", "three")
}
Esc & t::Esc
Esc & u::Send, !{F4}
; Esc & v::Send, ^v
Esc & w::#2
Esc & x::Esc
Esc & y::Esc
Esc & z::Esc
Esc & tab::Send, ^{tab}
Esc & `;::Send, !^i
; Esc & Space::Send, #{Up}
Esc & Space::
wingetpos, X,Y,Width,Height, A
if(Width >= 2500 and Height >= 1350){
    send, #{Down}
}else{
    send, #{Up}
}
; msgbox, the active window is at %X%, %Y%
; msgbox, the active window is width %Width%, height %Height%
return
Esc & Enter::
if(global DESKIDX == 1){
    Send, ^#{Right}
    global DESKIDX = 2
}
else{
    Send, ^#{Left}
    global DESKIDX = 1
}
return

; {
;     Detecthiddenwindows, on
;         if WinExist("ahk_exe Code.exe")
;             winActivate, ahk_exe Code.exe
;         else
;             Run, Code.cmd
;     return
; }
; {
;     Detecthiddenwindows, on
;         if WinExist("ahk_exe chrome.exe")
;             winActivate, ahk_exe chrome.exe
;         else
;             Run, chrome.exe
;     return
; }

SwitchIME(dwLayout){
    HKL:=DllCall("LoadKeyboardLayout", Str, dwLayout, UInt, 1)
    ControlGetFocus,ctl,A
    SendMessage,0x50,0,HKL,%ctl%,A
}

#If WinActive("ahk_exe SumatraPDF.exe")
{
    k::send, {Up}{Up}{Up}{Up}
    i::send, {Down}{Down}{Down}{Down}
    j::send, ^+{Tab}
    l::send, ^{Tab}
    +k::send, {PgUp}
    +i::send, {PgDn}
}
#IF

#If WinActive("ahk_exe 全球学术快报.exe")
{
    k::send, {Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}
    j::send, {Left}{Left}{Left}{Left}{Left}{Left}{Left}{Left}{Left}{Left}
    i::send, {Down}{Down}{Down}{Down}{Down}{Down}{Down}{Down}{Down}{Down}
    l::send, {Right}{Right}{Right}{Right}{Right}{Right}{Right}{Right}{Right}{Right}
    +k::send, {PgUp}
    +i::send, {PgDn}
}
#IF

#If WinActive("ahk_exe CAJVieweru.exe")
{
    k::send, {Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}
    j::send, {Left}{Left}{Left}{Left}{Left}{Left}{Left}{Left}{Left}{Left}
    i::send, {Down}{Down}{Down}{Down}{Down}{Down}{Down}{Down}{Down}{Down}
    l::send, {Right}{Right}{Right}{Right}{Right}{Right}{Right}{Right}{Right}{Right}
    +k::send, {PgUp}
    +i::send, {PgDn}
}
#IF

#If WinActive("ahk_exe msedge.exe")
{
    Esc::
        send, {ESC}
        SWITCHIME(04090409)
    RETURN
    !q::send, ^w
    !j::send, ^+{Tab}
    !l::send, ^{tab}
    !Enter::send, {F11}
}
#IF

#If WinActive("ahk_exe chrome.exe")
{
    !q::send, ^w
    !j::send, ^+{Tab}
    !l::send, ^{tab}
    !Enter::send, {F11}
}
#IF

#If WinActive("ahk_exe Code.exe")
{
    Esc & l::
    send, !xs
    send, !xp
    send, {ESC}
    return
    Esc::
        ; send, !xs
        ; send, !xp
        ; send, {ESC}
        send, {ESC}
        SWITCHIME(04090409)
    return
    Escape & b::^b
    !e::Send, /
}
#IF

#If WinActive("ahk_class SunAwtFrame")
{
    ^f::send, !{F12}
    !e::Send, /
}
#IF

#If WinActive("ahk_exe alacritty.exe")
{
    Esc::
        send, {ESC}
        SWITCHIME(04090409)
    RETURN
}
#IF

#If WinActive("ahk_exe Wechat.exe")
{
    Esc::RETURN
    !q::Send, ^{Up}
    !w::Send, ^{Down}
}
#IF

#If WinActive("ahk_exe WechatStore.exe")
{
    Esc::RETURN
    !q::Send, ^{Up}
    !w::Send, ^{Down}
}
#IF

#If WinActive("ahk_exe Pal2_x64.exe")
{
    a::Click
}
#IF

#If WinActive("ahk_exe putty.exe")
{
    Esc::
        send, {ESC}
        SWITCHIME(04090409)
    RETURN
}
#IF


#If WinActive("ahk_exe windowsTerminal.exe")
{
    Esc::
        sendinput, {ESC}
        ; send, ^9
        SWITCHIME(04090409)
    return
    ; SWITCHIME(04090409)
}
#IF

global MYKEYTOGGLE := 0
#If WinActive("ahk_exe mhtab.exe")
{
    Esc::
        if(global MYKEYTOGGLE == 0) {
            global MYKEYTOGGLE = 1
        }else{
            global MYKEYTOGGLE = 0
        }
        send, {Esc}
    return
    1::
        if(global MYKEYTOGGLE == 0) {
            send, {f1}
        }else{
            send, 1
        }
    return
    g::
        if(global MYKEYTOGGLE == 0) {
            send, !g
        }else{
            send, d
        }
    return
    2::
        if(global MYKEYTOGGLE == 0) {
            send, {f2}
        }else{
            send, 2
        }
    return
    z::
        if(global MYKEYTOGGLE == 0) {
            send, !o
        }else{
            send, z
        }
    return
    d::
        if(global MYKEYTOGGLE == 0) {
            Click right
        }else{
            send, s
        }
    return
    f::
        if(global MYKEYTOGGLE == 0) {
            send, {LButton}
        }else{
            send, t
        }
    return
    a::
        if(global MYKEYTOGGLE == 0) {
            send, !e
        }else{
            send, a
        }
    return
    s::
        if(global MYKEYTOGGLE == 0) {
            send, !w
        }else{
            send, r
        }
    return
    e::
        if(global MYKEYTOGGLE == 0) {
            send, !q
        }else{
            send, f
        }
    return
    x::
        if(global MYKEYTOGGLE == 0) {
            send, !h
            send, {f9}
        }else{
            send, x
        }
return
    Space::send, {Space}
    Space & e::!e
    Space & w::!w
    Space & q::!q
    Space & a::!a
}
#IF

; Capslock
; Capslock::Escape
!F9::
    WinGetPos, X, Y, W, H, A
    MsgBox, A is at %X%`,%Y% and its size is %W%x%H%
    ; if(X<=0 && Y <= 0) {
    ;     Send, #{Down}
    ; }

F10::
    taken := []
    input := "arstarstarst"
    Loop, Parse, input
    {
        msgbox, %A_LoopField%
        taken[A_LoopField] := (StrLen(taken[A_LoopField])?taken[A_LoopField]+1:1)
    }
